<template>
  <!--首页头部页面的搜索框部分 -->
  <div>
    <div class="head-container">
      <div class="head border-bottom">
        <!-- 搜索页面头部的小箭头 -->
        <div class="head-left">
          <img
            src="~assets/images/login/jiantou.png"
            alt=""
            @click="itemClick"
          />
        </div>
        <!-- 搜索页面头部的input框 -->
        <div class="head-center">
          <div class="left">
            <p>商品</p>
            <img src="~assets/images/home/jiantou-btn.png" alt="" />
          </div>
          <div class="center">
            <input
              type="text"
              placeholder="请输入关键字"
              v-model="keyword"
              ref="getValue"
            />
          </div>
        </div>
        <!-- 搜索按钮 -->
        <div class="head-right">
          <p @click="search">搜索</p>
        </div>
      </div>

      <!--首页头部页面的搜索列表部分 -->
      <div class="head-body">
        <div class="head-title">历史记录</div>
        <div
          class="head-body-item border-top"
          v-for="(item, index) in historyList"
          :key="index"
        >
          <img src="~assets/images/home/search-icon.png" alt="" />
          <span>{{ item }}</span>
        </div>
        <div class="clear" @click="cleartHistory">
          <img src="~assets/images/home/清除搜索历史@2x.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      keyword: "", //搜索框输入的值，一开始显示为空
      historyList: [], //历史搜索记录，存本地
    }
  },
  mounted() {
    //如果本地存储的数据historyList有值，直接赋值给data中的historyList
    if (JSON.parse(localStorage.getItem("historyList"))) {
      this.historyList = JSON.parse(localStorage.getItem("historyList"));
    }
  },
  methods: {
    search() {
      //如果搜索框为空提示
      if (this.keyword == "") {
        this.$toast.show('搜索内容不能为空!')
        return false;
      } else {
        /* 保持搜索历史记录只有五个数据如果  this.historyList的长度大于等于5，就删除数组中的最后一个元素*/
        if(this.historyList.length >= 5) {
          this.historyList.pop()
          localStorage.setItem('historyList',JSON.stringify(this.historyList));
        }
        // 没有搜索记录，把搜索值push进数组首位，存入本地
        if (!this.historyList.includes(this.keyword)) {
          this.historyList.unshift(this.keyword);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        } else {
          //有搜索记录，删除之前的旧记录，将新搜索值重新push到数组首位，indexOf返回值是在数组中元素首次出现的下标，如果没有返回-1
          let i = this.historyList.indexOf(this.keyword);
          //splice从位置为i的元素开始删除，删除一个
          this.historyList.splice(i, 1);
          //并将前面搜索重复的这个元素放在数组的首位
          this.historyList.unshift(this.keyword);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        }
        
        //添加完后将搜索框置为空
        this.keyword = "";
      }
    },
    //清除l搜索历史记录
    cleartHistory() {
      if( this.historyList.length === 0) {
        return
      }else{
         //清除存储在localStorage中的historyList，
        localStorage.removeItem("historyList");
        this.historyList = [];
        this.$toast.show('清空历史搜索成功');
        console.log(this.historyList.length)
      }
     
    },

    /* 点击顶部的小箭头返回到首页 */
    itemClick() {
      this.$router.push("/home");
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  display: flex;
  height: 0.44rem;
  line-height: 0.44rem;
  .head-left {
    img {
      display: inline-block;
      width: 0.08rem;
      height: 0.15rem;
      margin-left: 0.15rem;
    }
  }
  .head-center {
    width: 2.27rem;
    height: 0.3rem;
    display: flex;
    line-height: 0.3rem;
    margin-top: 0.07rem;
    align-content: center;
    margin-left: 0.215rem;
    border: 1px solid #ccc;
    .left {
      display: flex;
      margin-left: 0.185rem;
      p {
        font-size: 0.14rem;
        color: #333;
        display: block;
        margin-bottom: 0.18rem;
      }
      img {
        width: 0.08rem;
        height: 0.05rem;
        margin-top: 0.135rem;
        margin-left: 0.055rem;
        margin-right: 0.085rem;
        display: inline-block;
      }
    }
    .center {
      input {
        width: 0.84rem;
        height: 0.13rem;
        font-size: 0.14rem;
        margin-top: -.04rem;
      }
    }
  }
  .head-right {
    margin-left: 0.39rem;
    p {
      color: #333;
      font-size: 0.15rem;
    }
  }
}

.head-body {
  margin-left: 0.16rem;
  margin-right: 0.16rem;
  .head-title {
    height: 0.4rem;
    line-height: 0.4rem;
    color: #999;
    font-size: 0.15rem;
  }
  .head-body-item {
    height: 0.4rem;
    display: flex;
    align-items: center;
    img {
      display: inline-block;
      width: 0.15rem;
      height: 0.15rem;
      margin-right: 0.09rem;
    }
    span {
      color: #666;
      font-size: 0.13rem;
    }
  }
  .clear {
    top: 3.16rem;
    left: 1.19rem;
    width: 1.37rem;
    height: 0.43rem;
    position: fixed;
    img {
      width: 100%;
    }
  }
}

.search-content {
  z-index: 1;
  overflow: hidden;
  position: absolute;
  top: 0.44rem;
  left: 0;
  right: 0;
  bottom: 0;
  background: #eee;
  li {
    font-size: 0.14rem;
    color: #333;
  }
  .search-item {
    line-height: 0.31rem;
    padding-left: 0.1rem;
    background: #fff;
    color: #666;
  }
  .center {
    padding-top: 0.24rem;
    text-align: center;
    font-size: 0.16rem;
    color: #d20a0a;
  }
}
</style>
